<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ServiceFavorites.class.php, $Author:  $ */



/** Service for working with favorites
 * 
 * @package module.account
 * @version $Id: PhpFox_ServiceFavorites.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 *   
 */
class PhpFox_Mod_Account_ServiceFavorites extends BaseService 
{
    /** Get user favorites account
     * @param PhpFox_Mod_Account_ItemAccount user account object
     * @return array freinds list
     */
	function getFavorites($oAcc, $iPage=0, $iPageSize=0, $sSort='')
	{
	    $iCnt = $oAcc->oDb->getField('SELECT COUNT(*) FROM '.App::getT('favorite').
                                     '  WHERE user1 = "'.$oAcc->aData['user'].'"');
	    
        $aRows = array();
        if ($iCnt)
        {
            $iOffset = DbItem::_getOffset($iPage, $iPageSize, $iCnt);
            $sSql = 'SELECT u.*, ui.total AS rating '.
                    ' FROM `'.$oAcc->sTable.'` AS u '.
            		'   INNER JOIN `'.App::getT('favorite').'` AS f ON (u.user = f.user) '.
                    '   LEFT JOIN `'.App::getT('user_info').'` AS ui ON  (u.user = ui.user) '.
                    ' WHERE f.user1 = "'.$oAcc->aData['user'].'"'.
                    ($sSort?(' ORDER BY '.$sSort):'').
                    ($iPageSize?(' LIMIT '.$iOffset.','.$iPageSize):'');
            $aRows = $oAcc->oDb->getRows($sSql);
        }
        return array($aRows, $iCnt);	
	}
	
	/** Add target user to favorites.
	 * @param PhpFox_Mod_Account_ItemAccount $oAcc user who add another user
	 * @param PhpFox_Mod_Account_ItemAccount $oTargetAcc user who will be added
     */
	function addToFavorite(&$oAcc, &$oTargetAcc)
	{
	    //chekc for dublicat
        $iCnt = $oAcc->oDb->getField('SELECT COUNT(*) FROM '.App::getT('favorite').
                                     '  WHERE user1 = "'.$oAcc->aData['user'].'"'.
                                     '    AND user = "'.$oTargetAcc->aData['user'].'"');
        if ($iCnt)
        {
        	return false;
        }
	    
	    $aVals = array(
	    	'user1' => $oAcc->aData['user'],
	    	'user'  => $oTargetAcc->aData['user'],
	    	'time'  => time(),
	    );
	    return $oAcc->oDb->insert(App::getT('favorite'), $aVals);
	}
	
	/** Add target user to favorites.
	 * @param PhpFox_Mod_Account_ItemAccount $oAcc        who add another user
	 * @param PhpFox_Mod_Account_ItemAccount $oTargetAcc who will be added
     */
	function deleteFromFavorite($oAcc, $oTargetAcc)
	{
	    return $oAcc->oDb->query('DELETE FROM '.App::getT('favorite').
	                      '  WHERE user1 = "'.$oAcc->aData['user'].'"'.
	                      '    AND user = "'.$oTargetAcc->aData['user'].'"');
	}
	
}
?>